package com.feng.dao;

import org.junit.Test;

import java.sql.*;

public class BaseDao {

    private static String driver = "com.mysql.jdbc.Driver";
    private static String url = "jdbc:mysql://8.135.11.45/subsystem?useSSL=true&useUnicode=true&characterEncoding=utf8";
    private static String username = "subsystem";
    private static String password = "666666";

    public static Connection getConnection() {
        Connection connection = null;
        try {
            Class.forName(driver);
            connection = DriverManager.getConnection(url, username, password);
            System.out.println("成功获取连接!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return connection;

    }


    public static boolean CloseResources(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet) {
        Boolean flag = true;

        if (connection != null) {
            try {
                connection.close();
                connection = null;
            } catch (SQLException e) {
                e.printStackTrace();
                flag = false;
            }
        }

        if (resultSet != null) {
            try {
                resultSet.close();
                resultSet = null;
            } catch (SQLException e) {
                e.printStackTrace();
                flag = false;
            }
        }

        if (preparedStatement != null) {
            try {
                preparedStatement.close();
                preparedStatement = null;
            } catch (SQLException e) {
                e.printStackTrace();
                flag = false;
            }
        }

        return flag;
    }
    
    public static void main(String[] args) {
    	 Connection connection = getConnection();
	}

    @Test
    public void test() {
        Connection connection = getConnection();
        CloseResources(connection, null, null);
    }

}
